package com.conversationboard.view;

import java.text.SimpleDateFormat;

import org.joda.time.DateTime;

import com.conversationboard.config.Configuration;
import com.conversationboard.model.Board;
import com.conversationboard.model.Role;
import com.conversationboard.model.User;

public class RecentHistoryDisplay {

	public static String display(Board board, User user) {

		if (user.getRole() == Role.Anonymous) {
			return "";
		}

		StringBuilder buffer = new StringBuilder();

		buffer.append("<p>Threads Last Updated:</p>");

		/* Display a list of days from the past two weeks */

		DateTime date = new DateTime();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

		buffer.append("<p>");
		String howLongAgo = "1 day ago";

		for (int i = 0; i < 14; i++) {
			date = date.minusDays(1);
			String formattedDate = formatter.format(date.toDate());
			buffer.append("<a href=\"" + Configuration.getInstance().getRoot() + "/NewAnswersOnDateControllerServlet?boardid=" + board.getBoardId() + "&amp;date=" + formattedDate + "\">" + howLongAgo + "</a><br />");

			howLongAgo = (i + 2) + " days ago";
		}

		buffer.append("</p>");

		buffer.append("<p>");
		buffer.append("<form action=\"" + Configuration.getInstance().getRoot() + "/NewAnswersOnDateControllerServlet\" method=\"post\">");
		buffer.append("<input name='boardid' type='hidden' value='" + board.getBoardId() + "' />");
		buffer.append("<label for='date'>Or a given date (Year-month-day, e.g. 2000-10-10)</label> ");
		buffer.append("<input name='date' class='tinytextbox' type='text' size=35>");
		buffer.append("</form></p>");

		return buffer.toString();

	}

}
